package cn.ynrcb.personnelfile.admin.service.impl;

import cn.hutool.core.convert.Convert;
import cn.ynrcb.personnelfile.admin.dao.NoticeSendDao;
import cn.ynrcb.personnelfile.admin.entity.NoticeSendEntity;
import cn.ynrcb.personnelfile.admin.service.NoticeSendService;
import cn.ynrcb.personnelfile.common.utils.PageUtils;
import cn.ynrcb.personnelfile.sysadmin.service.CurrentUserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("noticeSendService")
public class NoticeSendServiceImpl extends ServiceImpl<NoticeSendDao, NoticeSendEntity> implements NoticeSendService {

    @Autowired
    private CurrentUserService currentUserService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<NoticeSendEntity> page =
                new Page<NoticeSendEntity>(Convert.toLong(params.get("page"), 1L), Convert.toLong(params.get("limit"), 10L));
        List<NoticeSendEntity> list = baseMapper.queryListPage(page, params);
        page.setRecords(list);
        return new PageUtils(page);
    }

    @Override
    public List<NoticeSendEntity> queryList(Map<String, Object> params) {
        return baseMapper.queryList(params);
    }

    @Override
    public void updateNoticeSendRead(Long id, Long userId) {
        baseMapper.setRead(id, userId, new Date());
    }

    @Override
    @Transactional
    public void markRead(String ids) {
        String[] idArray = ids.split(",");
        for(String id : idArray){
            baseMapper.markRead(Convert.toLong(id),currentUserService.getUserId(),new Date());
        }
    }

    @Override
    public Integer needReadNum(Long id) {
        return baseMapper.needReadNum(id);
    }

    @Override
    public void deleteNoticeSend(Integer[] ids) {
        baseMapper.deleteNoticeSend(Arrays.asList(ids));

    }

    @Override
    public void markAllReaded(Long currentUserId) {
        baseMapper.markAllReaded(currentUserId);

    }

}
